package com.hoshiibuy.spu.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.spu.entity.ExtSpu;
import com.hoshiibuy.spu.param.ExtSpuPageParam;
import com.hoshiibuy.spu.param.ExtSpuUpdateParam;
import com.hoshiibuy.spu.service.IExtSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * SPU营销资料controller类
 *
 * @author : yangcheng
 * @date : 2019/4/30 16:49
 * @mail : maiboyc@163.com
 **/
@RestController
@RequestMapping("/extSpu")
@Api(value = "SPU营销资料管理", tags = {"SPU营销资料操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class ExtSpuController extends BaseController {

    // SPU基本资料service
    @Autowired
    private IExtSpuService extSpuService;

    /**
     * 根据ID查询spu营销资料信息
     * @param id spu营销资料ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询spu营销资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "spu营销资料主键ID", required = true, dataType = "String", example = "3187243209717710848", paramType = "query")
    public Response get(@PathVariable ("id") Long id) {
        return Response.data(extSpuService.get(id));
    }

    /**
     * 根据名称查询spu营销资料信息
     * @param name  spu营销资料标题
     * @return Response
     */
    @GetMapping("/find/{name}")
    @ApiOperation(value = "根据名称查询", notes = "根据名称查询spu营销资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "name", value = "spu基本资料标题", required = true, dataType = "String", example = "衬衫", paramType = "query")
    public Response findSpuByName(String name) {
        Wrapper wrapper = Wrappers.<ExtSpu>query().lambda().like(ExtSpu::getTitle, name);
        return Response.data(extSpuService.getOne(wrapper));
    }

    /**
     * 分页查询spu营销资料信息（get请求，以具体值接收参数）
     * @param pageIndex  当前页
     * @param pageSize   每页大小
     * @param title      spu标题
     * @param serviceDescription   售后说明
     * @param notice   购买须知
     * @return Response
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询spu营销资料信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true, dataType = "Long", example = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true, dataType = "Long", example = "10", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "spu标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "serviceDescription", value = "售后说明", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "notice", value = "购买须知", dataType = "String", paramType = "query")
    })
    public Response list(Long pageIndex, Long pageSize, String title, String serviceDescription, String notice) {
        ExtSpu entity = new ExtSpu();
        entity.setPageIndex(pageIndex);
        entity.setPageSize(pageSize);
        entity.setTitle(title);
        entity.setServiceDescription(serviceDescription);
        entity.setNotice(notice);
        return Response.data(extSpuService.List(entity));
    }

    /**
     * 分页查询spu营销资料信息（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询spu营销资料信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response list(@RequestBody ExtSpuPageParam param) {
        ExtSpu entity = new ExtSpu();
        BeanUtils.copyProperties(param, entity);
        return Response.data(extSpuService.List(entity));
    }

    /**
     * 新增spu营销资料
     * @param entity 保存参数实体
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增spu营销资料", notes = "新增spu营销资料", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response saveSpu(@RequestBody @Valid ExtSpu entity, BindingResult result) {
        Response rs = new Response();
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                rs = Response.data(extSpuService.save(entity));
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 修改spu营销资料
     * @param param 修改参数实体
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改spu营销资料", notes = "修改spu营销资料", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response updateSpuById(@RequestBody @Valid ExtSpuUpdateParam param, BindingResult result) {
        Response rs = new Response();
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                ExtSpu entity = new ExtSpu();
                BeanUtils.copyProperties(param, entity);
                rs = Response.data(extSpuService.update(entity));
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 根据ID删除spu营销资料信息
     * @param id spu营销资料ID
     * @return Response
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除spu营销资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "spu营销资料主键ID", required = true, dataType = "String", example = "3187243209717710848", paramType = "query")
    public Response deleteById(Long id) {
        return Response.data(extSpuService.delete(id));
    }
}  